<template>
  <div>
    <!--  面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片视图区域-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.userName"
            clearable
            @clear="userlist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户
          </el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="用户名" prop="userName"></el-table-column>
        <el-table-column label="角色" prop="userPower">
          <template slot-scope="scope">
            <span v-if="scope.row.userPower == 2">普通用户</span>
            <span v-if="scope.row.userPower == 1">管理员</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" prop="userEmail"></el-table-column>
        <el-table-column label="居住地" prop="userLocation"></el-table-column>
        <el-table-column
          label="求职意向"
          prop="userIntention"
        ></el-table-column>
        <el-table-column label="是否被冻结" prop="frozen">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.frozen"
              @change="userStateChanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
            ></el-button>
            <!--删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.userId)"
            ></el-button>
            <!--          &lt;!&ndash;分配角色按钮&ndash;&gt;-->
            <!--          <el-tooltip  effect="dark" content="分配角色" placement="top" :enterable="false">-->
            <!--            <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>-->
            <!--          </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>
      <!--    分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--  添加用户的对话框-->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogVisibleClosed"
    >
      <!--    内容主体区域-->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="110px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="addForm.userName"
            prefix-icon="el-icon-user-solid"
            clearable
          ></el-input>
        </el-form-item>
        <!--  密码    -->
        <el-form-item label="密码" prop="userPassword">
          <el-input
            v-model="addForm.userPassword"
            prefix-icon="el-icon-lock"
            type="password"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input
            v-model="addForm.checkPassword"
            prefix-icon="el-icon-lock"
            type="password"
            clearable
          ></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="userEuserEmail">
          <el-input
            v-model="addForm.userEuserEmail"
            prefix-icon="el-icon-message"
            type="userEuserEmail"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <!--    底部区域-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!--  修改用户的对话框-->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogVisibleClosed"
    >
      <!--    内容主体区域-->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名">
          <el-input
            v-model="editForm.userName"
            prefix-icon="el-icon-user-solid"
            disabled
          ></el-input>
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="userEmail">
          <el-input
            v-model="editForm.userEmail"
            prefix-icon="el-icon-message"
            clearable
          ></el-input>
        </el-form-item>
        <!--  居住地    -->
        <el-form-item label="居住地" prop="userLocation">
          <el-input
            v-model="editForm.userLocation"
            prefix-icon="el-icon-userLocation-outline"
            clearable
          ></el-input>
        </el-form-item>
        <!--  求职意愿    -->
        <el-form-item label="意向" prop="userIntention">
          <el-input
            v-model="editForm.userIntention"
            prefix-icon="el-icon-star-off"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="editForm.userPower">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!--    底部区域-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.addForm.userPassword !== '') {
          if (
            this.addForm.userPassword.length <= 4 ||
            this.addForm.userPassword.length >= 14
          ) {
            callback(new Error('密码的长度在5-15个字符之间'))
          } else {
            this.$refs.addFormRef.validateField('checkPassword')
          }
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.addForm.userPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const checkuserEuserEmail = (rule, value, callback) => {
      const userEmailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (value === '') {
        return callback(new Error('邮箱不能为空'))
      }
      if (userEmailReg.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的邮箱格式'))
      }
    }
    return {
      // select选项
      options: [
        {
          value: '1',
          label: '管理员',
        },
        {
          value: '2',
          label: '普通用户',
        },
      ],
      // 获取用户列表的参数对象
      queryInfo: {
        userName: '',
        // 当前的页数
        pageIndex: 1,
        // 当前每页显示多少条数据
        pageSize: 10,
      },
      userlist: [],
      total: 0,
      // 控制添加用户框的显示与隐藏
      addDialogVisible: false,
      // 控制修改用户框的显示与隐藏
      editDialogVisible: false,
      // 添加用户表单
      addForm: {
        userName: '',
        userPassword: '',
        checkPassword: '',
        userEuserEmail: '',
      },
      // 修改用户信息表单
      editForm: {},
      // 添加表单的验证规则对象
      addFormRules: {
        userName: [
          { required: true, message: '请输入注册用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        userPassword: [{ validator: validatePass, trigger: 'blur' }],
        checkPassword: [{ validator: validatePass2, trigger: 'blur' }],
        userEuserEmail: [{ validator: checkuserEuserEmail, trigger: 'blur' }],
      },
      // 修改用户信息表单的验证规则对象
      editFormRules: {
        userEmail: [{ validator: checkuserEuserEmail, trigger: 'blur' }],
        userLocation: [
          { required: true, message: '请输入用户居住地', trigger: 'blur' },
        ],
        userIntention: [
          { required: true, message: '请输入用户的求职意愿', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    var form = JSON.parse(window.sessionStorage.getItem('user'))
    if (form.userPower != 1) {
      this.$message.error('您没有权限访问')
      this.$router.push('/home')
    }
    this.getUserList()
  },
  methods: {
    getUserList() {
      var that = this
      that.$http.post('user/page', this.queryInfo).then(function (res) {
        if (res.data.code == 200) {
          that.userlist = res.data.data
          that.total = res.data.count
        } else {
          return that.$message.error('获取用户列表失败！')
        }
      })
    },
    // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getUserList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pageIndex = newPage
      this.getUserList()
    },
    // 监听 switch 开关状态的改变
    async userStateChanged(userinfo) {
      console.log(userinfo)
      const { data: res } = await this.$http.put(
        `${userinfo.id}/${userinfo.isFro}`
      )
      if (res.state !== 200) {
        userinfo.isFro = !userinfo.isFro
        return this.$message.error('用户状态更新失败！')
      }
      this.$message.success('更新用户状态成功！')
    },
    // 监听关闭添加用户对话框的的关闭事件
    addDialogVisibleClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 监听关闭修改用户对话框的的关闭事件
    editDialogVisibleClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 点击按钮，添加新用户
    addUser() {
      this.$refs.addFormRef.validate(async (vaild) => {
        if (!vaild) {
        }
        var that = this
        that.$http.post('user/register', this.addForm).then(function (res) {
          if (res.data.code == 200) {
            that.$message.success('添加成功')
            that.getUserList()
            that.addDialogVisible = false
          } else {
            return that.$message.error('添加失败！')
          }
        })
      })
    },
    // 展示编辑用户的对话框
    showEditDialog(row) {
      // console.log(row)
      this.editDialogVisible = true
      this.editForm = JSON.parse(JSON.stringify(row))
      // console.log(this.editForm)
    },
    // 修改信息并提交
    editUserInfo() {
      this.editDialogVisible = false
      this.$refs.editFormRef.validate(async (vaild) => {
        if (!vaild) {}
        var that = this
      that.$http.post('user/update', that.editForm).then(function (res) {
      if (res.data.code == 200) {
        that.getUserList();
        return that.$message.success('修改成功！')
      } else {
        return that.$message.error(res.data.msg)
      }
    })
      })
    },
    // 更加Id删除对应的用户信息
    async removeUserById(id) {
      // console.log(id)
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
        // 如果用户确认删除，则返回值为字符串 confirm
        // 如果用户取消删除，则返回值为字符串 cancel
        // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // console.log('确认了删除')
      
      var that = this
    that.$http.post('user/delete', {userId:id}).then(function (res) {
      if (res.data.code == 200) {
        that.getUserList();
        return that.$message.success('删除成功！')
      } else {
        return that.$message.error('删除失败！')
      }
    })
    },
  },
}
</script>

<style scoped>
</style>
